package com.source.lightfowl.basedata.controller;

import com.alibaba.excel.EasyExcel;
import com.source.lightfowl.DTO.StallsDownloadDTO;
import com.source.lightfowl.DTO.StallsUploadDTO;
import com.source.lightfowl.VO.StallsVO;
import com.source.lightfowl.basedata.entity.Stalls;
import com.source.lightfowl.basedata.service.StallsService;
import com.source.lightfowl.listener.UploadExcelListener;
import com.source.lightfowl.syslog.annotation.LogAnnotation;
import com.source.lightfowl.utils.MyResult;
import com.source.lightfowl.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: cxy
 * @Description: 屠宰档口信息控制器
 */
@Api(value = "档口信息管理", tags = "档口信息管理")
@RestController
@CrossOrigin
@RequestMapping
public class StallsController {

    @Autowired
    private StallsService stallsService;


    /**
     * 下载Excel
     *
     * @param response
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "下载Excel")
    @GetMapping("/stalls/downloadExcel")
    public MyResult download(HttpServletResponse response) throws IOException {
        DownloadExcelHandler downloadExcelHandler = new DownloadExcelHandler();
        downloadExcelHandler.download(response, stallsService, StallsDownloadDTO.class);
        return new MyResult(0, "success");
    }

    /**
     * 文件下载并且失败的时候返回json（默认失败了会返回一个有部分数据的Excel）
     *
     * @param response
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "文件下载并且失败的时候返回json（默认失败了会返回一个有部分数据的Excel）")
    @GetMapping("/stalls/downloadFailedUsingJson")
    public MyResult downloadFailedUsingJson(HttpServletResponse response) throws IOException {
        DownloadExcelHandler downloadExcelHandler = new DownloadExcelHandler();
        downloadExcelHandler.downloadFailedUsingJson(response, stallsService, StallsUploadDTO.class);
        return new MyResult(0, "success");
    }

    /**
     * 上传Excel
     *
     * @param file
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "上传Excel")
    @PostMapping("/stalls/uploadExcel")
    @ResponseBody
    public MyResult upload(MultipartFile file, HttpServletRequest request) throws IOException {
        EasyExcel.read(file.getInputStream(), StallsUploadDTO.class, new UploadExcelListener(stallsService, request)).sheet().doRead();
        return new MyResult(0, "success");
    }

    //查找全部
    @ApiOperation(value = "查询全部数据", notes = "- 只查询审核通过的数据")
    @GetMapping("/stalls")
    public MyResult findAll() {
        List<StallsVO> all = stallsService.findAll(1);
        return MyResult.build(0, "成功", all);
    }

    //查找 分页
    @ApiOperation(value = "分页查询（传递两个参数：页数 个数/页）", notes = "- 只查询审核通过的数据")
    @GetMapping("/stallsFindAllPage")
    public MyResult findAllPage(int page, int pageNum) {
        Page<StallsVO> all = stallsService.findAllPageVO(1, page, pageNum);
        return MyResult.build(0, "成功", all);
    }

    //根据id查-新
    @ApiOperation(value = "根据id查询", notes = "- 查询所有状态的数据")
    @GetMapping("/stalls/id")
    public MyResult findById(Integer id) {
        StallsVO house = stallsService.findByIdVO(id);
        return MyResult.build(0, "成功", house);
    }

    //增加
    @ApiOperation(value = "增加 数据 ", notes = "- 等待管理员审核")
    @PostMapping("/stalls")
    @LogAnnotation(operEvent = "操作类型：添加", operType = 1)
    public MyResult create(Stalls stalls, MultipartFile file, HttpServletRequest request) {
        stalls.setContacts(stalls.getUserId());
        stalls.setState(2);
        stallsService.save(stalls, file, request);
        return MyResult.build(0, "成功");
    }

    //修改
    @ApiOperation(value = "修改 数据 ")
    @PostMapping("/stalls/update")
    @LogAnnotation(operEvent = "操作类型：修改", operType = 2)
    public MyResult update(Stalls stalls, MultipartFile file, HttpServletRequest request) {
        Stalls oldStalls = stallsService.findById(stalls.getId()).get();
        stalls.setState(oldStalls.getState());
        stallsService.save(stalls, file, request);
        return MyResult.build(0, "成功");
    }


    //删除
    @ApiOperation(value = "根据id删除数据", notes = "- 等待管理员审核")
    @DeleteMapping("/stalls/id")
    @LogAnnotation(operEvent = "操作类型：删除记录", operType = 3)
    public MyResult deleteById(Integer id) {
        Stalls stalls = stallsService.findById(id).get();
        stalls.setState(3);
        stallsService.save(stalls);
        return MyResult.build(0, "成功");

    }

    //删除 批量
    @ApiOperation(value = "批量删除数据：参数传递id的字符串 用英文的逗号分开")
    @DeleteMapping("/stallsDeleteBatch")
    @LogAnnotation(operEvent = "操作类型：批量删除记录", operType = 4)
    public MyResult deleteBatch(String ids) {    //注意：参数的分隔符逗号是英文的
        stallsService.changeBatch(3, ids);
        return MyResult.build(0, "成功");

    }

    //模糊查询
    @ApiOperation(value = "按名称模糊查询", notes = "- 只查询审核通过的数据")
    @GetMapping(value = "/stalls/likeName")
    public MyResult getStallsByLikeName(String name) {
        List<StallsVO> result = stallsService.getByLikeName(name);
        return MyResult.build(0, "成功", result);
    }

    /**
     * 管理员审核 - 发布通过
     * @param ids
     * @return
     */
    @ApiOperation(value = "管理员审核 - 发布通过 ", notes = "- 批量审核：参数传递id的字符串 用英文的逗号分开")
    @RequestMapping(value = "/stalls/agreePublishs", method = RequestMethod.POST)
    public MyResult agreePublishs(String ids) {
        stallsService.changeBatch(1, ids);
        return MyResult.build(0, "成功");
    }

    /**
     * 管理员审核 - 删除通过
     * @param ids
     * @return
     */
    @ApiOperation(value = "管理员审核 - 删除通过 ", notes = "- 批量审核：参数传递id的字符串 用英文的逗号分开")
    @RequestMapping(value = "/stalls/agreeDeleteS", method = RequestMethod.POST)
    public MyResult agreeDeleteS(String ids) {
        stallsService.deleteBatch(ids);
        return MyResult.build(0, "成功");
    }

    /**
     * 发布待审核列表
     * @return
     */
    @ApiOperation(value = "发布待审核列表")
    @GetMapping("/stalls/findAgreeing")
    public MyResult findAgreeing() {
        List<StallsVO> list = stallsService.findAll(2);
        return MyResult.build(0, "成功", list);
    }

    /**
     * 删除待审核列表
     * @return
     */
    @ApiOperation(value = "删除待审核列表")
    @GetMapping("/stalls/findDeleting")
    public MyResult findDeleting() {
        List<StallsVO> list = stallsService.findAll(3);
        return MyResult.build(0, "成功", list);
    }

    /**
     * 用户id查档口id
     * @param userId
     * @return
     */
    @ApiOperation(value = "用户id查档口id")
    @GetMapping("/stalls/findByUserId")
    public MyResult findByUserId(String userId) {
        List<String> list = stallsService.findByUserId(userId);
        return MyResult.build(0, "成功", list);
    }

    /**
     * 某档口使用标签数 - 当天
     * @param id
     * @return
     */
    @ApiOperation(value = "某档口使用标签数 - 当天")
    @GetMapping("/stalls/dailyRecord")
    public MyResult dailyRecord(Integer id) {
        return MyResult.build(0, "成功", stallsService.dailyRecord(id));
    }

    /**
     * 某档口使用标签数 - 当周
     * @param id
     * @return
     */
    @ApiOperation(value = "某档口使用标签数 - 当周")
    @GetMapping("/stalls/weeklyRecord")
    public MyResult weeklyRecord(Integer id) {
        return MyResult.build(0, "成功", stallsService.weeklyRecord(id));
    }

    /**
     * 某档口使用标签数 - 当月
     * @param id
     * @return
     */
    @ApiOperation(value = "某档口使用标签数 - 当月")
    @GetMapping("/stalls/monthlyRecord")
    public MyResult monthlyRecord(Integer id) {
        return MyResult.build(0, "成功", stallsService.monthlyRecord(id));
    }

    /**
     * 某档口使用标签数 - 当天 + 按父禽类统计
     * @param id
     * @return
     */
    @ApiOperation(value = "某档口使用标签数 - 当天 + 按父禽类统计")
    @GetMapping("/stalls/dailyRecordByPoultry")
    public MyResult dailyRecordByPoultry(Integer id) {
        return MyResult.build(0, "成功", stallsService.dailyRecordByPoultry(id));
    }

    /**
     * 某档口使用标签数 - 当周 + 按父禽类统计
     * @param id
     * @return
     */
    @ApiOperation(value = "某档口使用标签数 - 当周 + 按父禽类统计")
    @GetMapping("/stalls/weeklyRecordByPoultry")
    public MyResult weeklyRecordByPoultry(Integer id) {
        return MyResult.build(0, "成功", stallsService.weeklyRecordByPoultry(id));
    }

    /**
     * 某档口使用标签数 - 当月 + 按父禽类统计
     * @param id
     * @return
     */
    @ApiOperation(value = "某档口使用标签数 - 当月 + 按父禽类统计")
    @GetMapping("/stalls/monthlyRecordByPoultry")
    public MyResult monthlyRecordByPoultry(Integer id) {
        return MyResult.build(0, "成功", stallsService.monthlyRecordByPoultry(id));
    }

    /**
     * 某档口使用标签数 - 当天 + 按父禽类统计 + 降序
     * @param id
     * @return
     */
    @ApiOperation(value = "某档口使用标签数 - 当天 + 按去向市场 + 按父禽类统计 + 降序")
    @GetMapping("/stalls/dailyRecordByPoultryAndToMark")
    public MyResult dailyRecordByPoultryAndToMark(Integer id) {
        return MyResult.build(0, "成功", stallsService.dailyRecordByPoultryAndToMark(id));
    }

    /**
     * 某档口使用标签数 - 当周 + 按父禽类统计 + 降序
     * @param id
     * @return
     */
    @ApiOperation(value = "某档口使用标签数 - 当周 + 按去向市场 + 按父禽类统计 + 降序")
    @GetMapping("/stalls/weeklyRecordByPoultryAndToMark")
    public MyResult weeklyRecordByPoultryAndToMark(Integer id) {
        return MyResult.build(0, "成功", stallsService.weeklyRecordByPoultryAndToMark(id));
    }

    /**
     * 某档口使用标签数 - 当月 + 按父禽类统计 + 降序
     * @param id
     * @return
     */
    @ApiOperation(value = "某档口使用标签数 - 当月 + 按去向市场 + 按父禽类统计 + 降序")
    @GetMapping("/stalls/monthlyRecordByPoultryAndToMark")
    public MyResult monthlyRecordByPoultryAndToMark(Integer id) {
        return MyResult.build(0, "成功", stallsService.monthlyRecordByPoultryAndToMark(id));
    }

}
